package com.max.utils;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;

public class CommonUtils {

    public static boolean isNotBlank(Object str) {
        if (str == null) {
            return false;
        }
        if (str instanceof String) {
            return ((String) str).length() > 0;
        }
        return true;
    }


    public static String fieldName2ColumnName(String fieldName) {
        if (fieldName == null || fieldName.length() == 0) {
            return null;
        }
        StringBuilder newFieldName = new StringBuilder();
        String lowerFieldName = fieldName.toLowerCase();
        for (int i = 0, len = fieldName.length(); i < len; i++) {
            char oldChar = fieldName.charAt(i);
            char lowerChar = lowerFieldName.charAt(i);
            if (oldChar == lowerChar) {
                newFieldName.append(oldChar);
            } else {
                newFieldName.append("_").append(lowerChar);
            }
        }
        return newFieldName.toString();
    }


    public static <T> void setFiledValue(T t, Field field, ResultSet resultSet) throws IllegalAccessException, SQLException {
        String filedName = field.getName();
        String fieldTypeSimpleName = field.getType().getSimpleName();
        field.setAccessible(true);
        switch (fieldTypeSimpleName) {
            case "BigDecimal" -> {
                BigDecimal bigDecimal = resultSet.getBigDecimal(fieldName2ColumnName(filedName));
                field.set(t, bigDecimal);
            }
            case "int", "Integer" -> {
                int anInt = resultSet.getInt(fieldName2ColumnName(filedName));
                field.set(t, anInt);
            }
            case "long", "Long" -> {
                long aLong = resultSet.getLong(fieldName2ColumnName(filedName));
                field.set(t, aLong);
            }
            case "double", "Double" -> {
                double aDouble = resultSet.getDouble(fieldName2ColumnName(filedName));
                field.set(t, aDouble);
            }
            case "float", "Float" -> {
                float aFloat = resultSet.getFloat(fieldName2ColumnName(filedName));
                field.set(t, aFloat);
            }
            case "boolean", "Boolean" -> {
                boolean aBoolean = resultSet.getBoolean(fieldName2ColumnName(filedName));
                field.set(t, aBoolean);
            }
            case "char", "Character" -> {
                char aChar = resultSet.getString(fieldName2ColumnName(filedName)).charAt(0);
                field.set(t, aChar);
            }
            case "byte", "Byte" -> {
                byte aByte = resultSet.getByte(fieldName2ColumnName(filedName));
                field.set(t, aByte);
            }
            case "short", "Short" -> {
                short aShort = resultSet.getShort(fieldName2ColumnName(filedName));
                field.set(t, aShort);
            }
            case "String" -> {
                String string = resultSet.getString(fieldName2ColumnName(filedName));
                field.set(t, string);
            }
            case "Date" -> {
                Date date = resultSet.getDate(fieldName2ColumnName(filedName));
                field.set(t, date);
            }
            default -> {
                Object o = resultSet.getObject(fieldName2ColumnName(filedName));
                field.set(t, o);
            }
        }
        field.setAccessible(false);
    }


}
